#-*- encoding:utf-8 -*-

from django.shortcuts import render
from django.contrib.auth import login
from django.http import HttpResponseRedirect

from forms import LoginForm


def index(request):
    """首页"""
    return HttpResponseRedirect('/ci/server_view/')


def login_(request):
    """登录系统"""
    context = {}
    if request.method == 'POST':
        form = LoginForm(request, request.POST)
        if form.is_valid():
            user = form.get_user()
            if user:
                login(request, user)
                if form.get_auto_login():   #set session
                    request.session.set_expiry(None)
                request.session['user'] = user
                return HttpResponseRedirect('/ci/server_view/')
        context['form'] = form

    else:
        form = LoginForm()
        context['form'] = form

    return render(request, 'xadmin/login.html', {'form': context['form']})


def logout_(request):
    """退出系统"""

    return HttpResponseRedirect('/login')